<template>
  <BasicTable @register="registerTable">
    <template #form-custom></template>
    <template #toolbar>
      <a-button v-if="hasPermission('resource_course_add')" type="primary" @click="handleCreate">
        新增
      </a-button>
    </template>
    <template #action="{ record }">
      <TableAction
        :actions="[
          {
            icon: 'clarity:note-edit-line',
            onClick: handleEdit.bind(null, record),
            auth: 'resource_course_edit',
          },
          {
            icon: 'ant-design:delete-outlined',
            color: 'error',
            popConfirm: {
              title: '是否确认删除',
              confirm: handleDelete.bind(null, record),
            },
            auth: 'resource_course_del',
          },
          {
            icon: 'ant-design:gold-outlined',
            label: '课时',
            onClick: handleLessonModify.bind(null, record),
            auth: 'resource_course_del',
          },
        ]"
      />
    </template>
  </BasicTable>
  <!--注册drawer  -->
  <CourseDrawer @register="registerDrawer" @success="handleSuccess" />

  <CourseLessonDrawer @register="registerLessonDrawer" @success="handleSuccess" />
</template>
<script lang="ts" setup>
  import { onMounted } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getBasicColumns, getFormConfig } from './courseTableData';
  //左侧拉板
  import { useDrawer } from '/@/components/Drawer';

  import { message } from 'ant-design-vue';
  import { getCourseByIdApi, getCoursePageApi, courseDelByIdApi } from '/@/api/resource/courseApi';
  import CourseDrawer from '/@/views/resource/course/courseDrawer.vue';
  import { usePermission } from '/@/hooks/web/usePermission';
  import CourseLessonDrawer from '@/views/resource/course/courseLessonDrawer.vue';

  const { hasPermission } = usePermission();
  const [registerDrawer, { openDrawer }] = useDrawer();

  const [registerLessonDrawer, { openDrawer: openLessonDrawer }] = useDrawer();

  const [registerTable, { reload }] = useTable({
    title: '课程管理',
    columns: getBasicColumns(),
    useSearchForm: true,
    formConfig: getFormConfig(),
    showTableSetting: true,
    rowKey: 'id',
    bordered: true,
    showIndexColumn: true,
    actionColumn: {
      width: 160,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
    api: getCoursePageApi,
    fetchSetting: {
      pageField: 'current',
      // 每页显示多少条
      sizeField: 'pageSize',
      // 请求结果列表字段  支持 a.b.c
      listField: 'records',
      totalField: 'total',
    },
  });

  function handleCreate() {
    openDrawer(true, {
      isUpdate: false,
    });
  }

  async function handleEdit(record: Recordable) {
    const data = await getCourseByIdApi(record.id);
    openDrawer(true, {
      data,
      isUpdate: true,
    });
  }

  async function handleDelete(record: Recordable) {
    await courseDelByIdApi(record.id);
    reload();
    message.success('删除成功');
  }

  async function handleLessonModify(record: Recordable) {
    const courseId = record.id;
    const courseName = record.title;
    openLessonDrawer(true, {
      courseId,
      courseName,
      isUpdate: true,
    });
  }

  function handleSuccess() {
    reload();
  }

  onMounted(() => {});
</script>
